#include "PlayLayer.h"
#include "RankLayer.h"
#include "MarkLayer.h"
#include "CardLayer.h"
#include "PlayerInfo.h"
#include "ConstClass.h"
#include "DataManage.h"
#include "AudioManage.h"
#include "ExitLayer.h"

#define PNG_BACK "Play/backGround.jpg"
#define PNG_MAP "Play/map.jpg"

PlayLayer::PlayLayer()
{
}


PlayLayer::~PlayLayer()
{
}

PlayLayer* PlayLayer::thisLayer;

Scene* PlayLayer::createScene()
{
	Scene* playScene = Scene::create();
	Layer* playLayer = PlayLayer::create();

	playScene->addChild(playLayer);

	return playScene;
}

bool PlayLayer::init()
{
	Layer::init();

	//string playerNames[5] = { "Haha", "TianX", "Fangsi", "Guan", "5km" };
	//PlayerInfo::initialPlayers(5, playerNames);
	DataManage::initialPlayers();

	Size size = Director::getInstance()->getVisibleSize();

	Sprite* backSprite = Sprite::create(PNG_BACK);
	backSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.5f));
	this->addChild(backSprite, 0);

	Sprite* mapSprite = Sprite::create(PNG_MAP);
	mapSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.57f));
	this->addChild(mapSprite, 0);

	this->rankLayer = RankLayer::create();
	this->addChild(rankLayer, 1);

	this->markLayer = MarkLayer::create();
	this->addChild(markLayer, 1);

	this->tipLayer = TipLayer::create();
	this->addChild(tipLayer, 1);

	CardLayer* cardLayer = CardLayer::create();
	this->addChild(cardLayer, 1);

	cardLayer->getMyMark(ConstClass::MY_T_INDEX);

	this->turtleManage = new TurtleManage(this);

	PlayLayer::thisLayer = this;

	AudioManage::playPlayMusic();

	PlayLayer::setKeyboardEnabled(true);
	PlayLayer::setKeypadEnabled(true);

	return true;
}

void PlayLayer::playCard(int cardID)
{
	DataManage::playCard(cardID, -1);

	this->markLayer->playCard(ConstClass::PLAYING_P_ID, cardID);
	float delay = this->markLayer->actionTime;

	this->turtleManage->moveTurtle(cardID,delay);

	this->rankLayer->changeRank(turtleManage->getRankChange(),delay);
	this->rankLayer->changeOverLap(turtleManage->getOverLap(),delay);

	nextPlayer();
}

void PlayLayer::playCard(int colorCardID, int cardID)
{
	DataManage::playCard(colorCardID, cardID);

	this->markLayer->playCard(ConstClass::PLAYING_P_ID, colorCardID, cardID);
	float delay = this->markLayer->actionTime;

	this->turtleManage->moveTurtle(cardID, delay);

	this->rankLayer->changeRank(turtleManage->getRankChange(), delay);
	this->rankLayer->changeOverLap(turtleManage->getOverLap(), delay);

	nextPlayer();
}

bool PlayLayer::checkCardEnable(int cardID)
{
	if (cardID < 10 && cardID > 4)
	{
		int tIndex = cardID % 5;
		return this->turtleManage->checkTurtleEnable(tIndex);
	}

	if (cardID == 16)
	{
		int cout = 0;
		for (int i = 0; i < 5; i++)
		{
			if (this->turtleManage->checkTurtleEnable(i))
			{
				cout++;
			}			
		}
		if (5 == cout)
		{
			return true;
		}
		else
		{
			return false;
		}		
	}

	return true;
}

int* PlayLayer::getLastTurtles()
{
	return this->turtleManage->getLastTurtles();
}

void PlayLayer::startRound()
{
	schedule(schedule_selector(PlayLayer::Timer_Tick), 1, kRepeatForever, 0);
	this->tipLayer->roundTip();
}

void PlayLayer::pauseRound()
{
	unschedule(schedule_selector(PlayLayer::Timer_Tick));
}

void PlayLayer::continueRound()
{
	if (this->turtleManage->checkTurtleWinner() > -1)
	{
		ConstClass::GAME_START = false;
		int wT = this->turtleManage->checkTurtleWinner();
		DataManage::getWinner(wT);
	}
	else
	{
		this->tipLayer->roundTip();
		schedule(schedule_selector(PlayLayer::Timer_Tick), 1, kRepeatForever, 0);
	}
}

void PlayLayer::Timer_Tick(float dt)
{
	if (ConstClass::MY_P_ID == ConstClass::PLAYING_P_ID)
	{
		pauseRound();
	}
	else
	{
		DataManage::getResult();
	}
}

void PlayLayer::nextPlayer()
{
	ConstClass::PLAYING_P_ID++;
	if (ConstClass::PLAYING_P_ID == ConstClass::PLAYER_COUNT)
	{
		ConstClass::PLAYING_P_ID = 0;
	}
}

void PlayLayer::reShuffle()
{
	this->tipLayer->reShuffleTip();
}

void PlayLayer::gameOver()
{
	pauseRound();
	this->tipLayer->gameOverTip();
	this->markLayer->showMarks();
}

void PlayLayer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE)
	{
		if (ConstClass::exiting)
		{
			ExitLayer* exit = (ExitLayer*)_exitLayer;
			exit->Back();
			return;
		}

		if (_exitLayer == nullptr)
		{
			Size size = Director::getInstance()->getVisibleSize();
			ExitLayer* exit = ExitLayer::create();
			exit->setPosition(Vec2(size.width / 2, size.height / 3 * 2));

			this->addChild(exit, 30);
			_exitLayer = exit;
		}
		else
		{
			ExitLayer* exit = (ExitLayer*)_exitLayer;
			exit->showExitLayer();
			_exitLayer->setVisible(true);
		}
	}
}